package com.eshipenet.shipnet.equipmentextbuilder

import org.joda.time.DateTime
import play.api.libs.json.{JsNull, JsValue, Json}

object EquipmentWithModelActionSerializer {
  private val schema = Json.obj(
    "type" -> "struct",
    "fields" -> Json.arr(
      Json.obj(
        "type"     -> "struct",
        "field"    -> "after",
        "name"     -> "EquipmentModel.Value",
        "optional" -> false,
        "fields" -> Json.arr(
          Json.obj(
            "type" -> "string",
            "name" -> "json"
          )
        )
      )
    )
  )

  def apply(action: EquipmentWithModelAction): List[(String, JsValue)] = {
    action match {
      case EquipmentWithModelInsert(after) =>
        val value = Json.obj(
          "schema" -> schema,
          "payload" -> Json.obj(
            "op" -> "i",
            "after" -> Json.obj(
              "equipment" -> after.equipment.raw,
              "model"     -> after.model.raw,
            ),
            "source" -> Json.obj(
              ),
            "ts_ms" -> DateTime.now().getMillis,
          )
        )
        val key = s"${action.id}.${action.modelId}"
        List((key, value))
      case EquipmentWithModelDelete(before) =>
        val value = Json.obj(
          "schema" -> Json.obj(),
          "payload" -> Json.obj(
            "op" -> "d",
            "before" -> Json.obj(
              "equipment" -> before.equipment.raw,
              "model"     -> before.model.raw,
            ),
            "source" -> Json.obj(
              ),
            "ts_ms" -> DateTime.now().getMillis,
          )
        )
        val key = s"${action.id}.${action.modelId}"
        List((key, value), (key, JsNull))
    }
  }
}
